package com.csx.kotlndemo.base;


import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;

import com.csx.kotlndemo.R;
import com.csx.kotlndemo.databinding.BaseLayoutBinding;
import com.csx.kotlndemo.util.ClassUtil;
import com.csx.kotlndemo.viewmodel.BaseViewModel;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

/**
 * 使用DataBinding，LiveData，考虑RxJava，一个顶级架构的设计师才能拿高工资。
 */
public abstract class BaseActivity<VM extends BaseViewModel,VB extends ViewDataBinding> extends AppCompatActivity {

    protected VM viewModel;
    /**视图绑定，可看做View*/
    BaseLayoutBinding baseLayoutBinding;
    protected VB viewBinding;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    public void setView(int layoutResID) {
//        viewBinding= DataBindingUtil.setContentView(this, layoutResID);
        baseLayoutBinding= DataBindingUtil.inflate(getLayoutInflater(), R.layout.base_layout,null,false);
         viewBinding = DataBindingUtil.inflate(getLayoutInflater(),layoutResID,null,false);
        FrameLayout fl= (FrameLayout) baseLayoutBinding.getRoot().findViewById(R.id.main_parent);
        fl.addView(viewBinding.getRoot());
        getWindow().setContentView(baseLayoutBinding.getRoot());
        initViewModel();
    }

    /**
     * 初始化ViewModel
     */
    private void initViewModel() {
        Log.d("initViewModel","initViewModel");
        Class<VM> viewModelClass = ClassUtil.getViewModel(this);
        if (viewModelClass != null) {
            this.viewModel = ViewModelProviders.of(this).get(viewModelClass);
        }
        viewModel.getError(this, new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                showError(o);
            }
        });
    }

    /**
     * 展示错误页面
     */
    public void showError(Object o){

    }



}
